package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.FirstRechargeRenewRemindService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.FirstRechargeRenewRemindSetting;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author cwj
 * @description
 * @date 2025/4/25 16:32
 **/
@Slf4j
@RestController
@RequestMapping("firstRechargeRenewRemind")
public class FirstRechargeRenewRemindController extends BaseController {


    @Resource
    private FirstRechargeRenewRemindService firstRechargeRenewRemindService;
    
    /**
     * @description 查询用户首充提醒/续费提醒的配置
     * @author cwj
     * @param [page]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/27 14:39
     **/
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<FirstRechargeRenewRemindSetting> pages = firstRechargeRenewRemindService.list(page);
        mv.addObject("firstRechargeRenewReminds", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("firstRechargeRenewRemind/first_recharge_renew_remind_list");
        return mv;
    }



    /**
     * @description 显示增肌充值提醒配置页面
     * @author cwj
     * @param []
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/27 15:53
     **/
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("firstRechargeRenewRemind/first_recharge_renew_remind_add");
        return mv;
    }


    /**
     * @description 增加充值提醒的配置信息
     * @author cwj
     * @param [firstRechargeRenewRemindSetting]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/27 16:08
     **/
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(FirstRechargeRenewRemindSetting firstRechargeRenewRemindSetting) {
        ModelAndView mv = new ModelAndView();
        firstRechargeRenewRemindService.add(firstRechargeRenewRemindSetting,this.getSessionUser(),this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }


    /**
     * @description
     * @author cwj
     * @param [id, type]
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/4/27 16:48
     **/
    @ResponseBody
    @RequestMapping("checkType")
    public Response<Object> checkType(@RequestParam(required = false) String id, Integer type) {
        FirstRechargeRenewRemindSetting firstRechargeRenewRemindSetting = firstRechargeRenewRemindService.queryByType(type);
        if (StringUtil.isBlank(id)) {
            if (!ObjectUtils.isEmpty(firstRechargeRenewRemindSetting)) {
                return Response.failedResponse("同类型的充值提醒已经存在，请修改");
            }
        } else {
            if (!ObjectUtils.isEmpty(firstRechargeRenewRemindSetting) && !id.equals(firstRechargeRenewRemindSetting.getId())) {
                return Response.failedResponse("同类型的充值提醒已经存在，请修改");
            }
        }
        return Response.successResponse();
    }

    /**
     * @description 编辑充值提醒配置信息
     * @author cwj
     * @param [id]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/27 16:41
     **/
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("firstRechargeRenewRemind", firstRechargeRenewRemindService.findById(new ObjectId(id)));
        mv.setViewName("firstRechargeRenewRemind/first_recharge_renew_remind_edit");
        return mv;
    }


    /**
     * @description 编辑充值提醒配置
     * @author cwj
     * @param [firstRechargeRenewRemindSetting]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/27 17:17
     **/
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(FirstRechargeRenewRemindSetting firstRechargeRenewRemindSetting) {
        ModelAndView mv = new ModelAndView();
        if(ObjectUtils.isEmpty(firstRechargeRenewRemindSetting) || StringUtils.isEmpty(firstRechargeRenewRemindSetting.getId())){
            mv.addObject("msg", "fail");
            mv.setViewName("save_result");
            return mv;
        }
        firstRechargeRenewRemindService.update(firstRechargeRenewRemindSetting, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
